var siloseer_Sidebar_Handle = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(Components.interfaces.nsIWindowWatcher);
Components.utils.import("resource://siloseer_gui/siloseer_shared.js");

var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                     .getInterface(Components.interfaces.nsIWebNavigation)
                     .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                     .rootTreeItem
                     .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                     .getInterface(Components.interfaces.nsIDOMWindow);


//this is the onclick for navigation links
//toggles highlight between navigation and content links
function select_nav_type(num)
{
	var newFields = document.getElementById('View_Config_Navigation').childNodes;
	$(newFields[2]).css("backgroundColor", "");
	$(newFields[3]).css("backgroundColor", "");
	
	global_select.type=0; // Set flag saying navigation selection has been chosen

	//alert('num is  ' + num + ' global is ' + global_select.nav_val);
	if(global_select.nav_val==num)
	{
		global_select.nav_val=-1; // nothing is selected
		$(newFields[2+num]).css("backgroundColor", ""); // 2+num directly selects node id
	}
	else
	{	
		global_select.nav_val = num;
		$(newFields[2+num]).css("backgroundColor", "#ff3fff");		
		
	}
	
	//Testing for NodeHash
	/*
	for(var i in nodeHash)
	{
		for(var j in  nodeHash[i])
		{
			alert('index is ' + i + 'key is ' + j + ' value is ' + nodeHash[i][j]);
		}
	}
	*/
}

//this is the onclick for navigation links
//toggles highlight between navigation and content links
function select_content_type(num)
{
	var cloneReadRootNodes = elementsById('clonereadroot');
	
	global_select.type=1; // Set flag saying navigation selection has been chosen
	
	for(var i=0;i<cloneReadRootNodes.length;i++)
	{
		$(cloneReadRootNodes[i]).css("backgroundColor", "");
	}
	
	if(global_select.con_val==num) //indicates that its already been selected hence unselect
	{
		global_select.con_val=-1; // nothing is selected
		$(cloneReadRootNodes[num]).css("backgroundColor", ""); // 2+num directly selects node id
	}
	else
	{	
		global_select.con_val = num;
		$(cloneReadRootNodes[num]).css("backgroundColor", "#ff3fff");		
	}
	
}


//start displaying values
function get_link_vals()
{
	//Navigation panel data
	var con_num = document.getElementById("content_links_num");
	con_num.textContent = domains[global_select.domain]['links']['content'].length;
			
	var nav_num = document.getElementById("nav_links_num");
	nav_num.textContent = domains[global_select.domain]['links']['nav'].length;
	
	var dom_name = document.getElementById("domain_name");
	dom_name.textContent = 'Domain: ' + domains[global_select.domain]['url'];
	
	moreFields(); // Content panel data

}

//Displaying tagged data for content panel in View Config
function moreFields() {
		
	var dArray = domains[global_select.domain]['data'];
	
	for(var i in dArray)
	{        
		var newFields = document.getElementById('readroot').cloneNode(true);
		
		newFields.id = 'clonereadroot';
		newFields.style.display = 'block';
		
		var newField = newFields.childNodes;
		
		//alert('main len is ' + dArray.length +' name is ' + dArray[i]['name'] + ' newfield len is ' + newField.length);
		
		for (var j=0;j<newField.length-1;j++) {
			var theName = newField[j].id;
			
			if(theName == "con_label")
			{
				newField[j].textContent = 'Name : '+dArray[i]['name'];

				newField[j].removeAttribute("onclick");
				newField[j].setAttribute("onclick","select_content_type("+i+")");
			}
				
					
			if(theName == "con_label_type")
				newField[j].textContent = 'Type : ' + dArray[i]['type'];
				
			if(theName == "con_label_num")
			{
				newField[j].id = 'con_label_num'+i;	
				newField[j].textContent = 'Tags: ' + dArray[i]['nodes'].length;
			}			
			
		} 	
		
		var insertHere = document.getElementById('writeroot');
		insertHere.parentNode.insertBefore(newFields,insertHere);
	}
}


function doPrediction(type)
{
	var XPathArray = [];
	
	if(type==0)
		XPathArray = doNavPrediction();
	else if(type==1)
		XPathArray = doContentPrediction();
	else
		return;
		
	var foundNodes = getNodesFromXPath(XPathArray);
	
	for (var j in foundNodes)
		$(foundNodes[j]).css("backgroundColor", "#3EFB88");
}

function doNavPrediction()
{  
  var nodes =[];
  var conNodes=[];
  var navNodes=[];
  var navConNodes = domains[global_select.domain]['links']['content'];
  var navNavNodes = domains[global_select.domain]['links']['nav'];
  
  //var navNodes = navConNodes.concat(navNavNodes); // merge 2 arrays
  
	for(var i=0;i<navConNodes.length-1;i++) {
	  var XPath1 = generateXPath(navConNodes[i]['dom']);
	  var XPath2 = generateXPath(navConNodes[i+1]['dom']);
	  
	  //alert('navcon xpath1 = ' + XPath1 + ' xpath2 = ' + XPath2);
	  
		var result_align = do_alignment(XPath1,XPath2); 
			
		conNodes.push(result_align);
	}
	
	if(navConNodes.length==1) conNodes.push(generateXPath(navConNodes[0]));
	
	//To populate suggested content links sites listbox	
	updateSuggestList(conNodes,'contentList');
	
	for(var i=0;i<navNavNodes.length-1;i++) {
	  var XPath1 = generateXPath(navNavNodes[i]['dom']);
	  var XPath2 = generateXPath(navNavNodes[i+1]['dom']);
	  
	  //alert('navnav xpath1 = ' + XPath1 + ' xpath2 = ' + XPath2);
	  
		var result_align = do_alignment(XPath1,XPath2); // do pairwise alignment -> return xpath string
		navNodes.push(result_align);
	}	
	
	if(navNavNodes.length==1) navNodes.push(generateXPath(navNavNodes[0]));
	
	updateSuggestList(navNodes,'navList');
	
	nodes = conNodes.concat(navNodes); // merge both nav and con selection nodes
	
	LOG('nodes size is ' + nodes.length + 'navnodes size is ' + navNodes.length + ' conNodes length is ' + conNodes.length);
	
	return nodes;
}

function doContentPrediction()
{
	var nodes =[];
  
	var dArray = domains[global_select.domain]['data'];
	
	for(var i in dArray)  
	{
		var curNodes = dArray[i]['nodes'];
		
		//for(var j=0;j<curNodes.length;j++)
			//alert(curNodes[j].textContent);
		 
		for(var j=0;j<curNodes.length-1;j++)
		{
			var XPath1 = generateXPath(curNodes[j]['dom']);
			var XPath2 = generateXPath(curNodes[j+1]['dom']);
			
			//alert('xpath1 = ' + XPath1 + ' xpath2 = ' + XPath2);
			//alert('curnodes = ' + curNodes[j]);
	  
			var result_align = do_alignment(XPath1,XPath2); // do pairwise alignment -> return xpath string
			//alert('i = ' + i + ' j = ' + j + ' val = ' + result_align);
			nodes.push(result_align);			 
		}
		
		if(curNodes.length==1) nodes.push(generateXPath(curNodes[0]));
	}	 
	
	return nodes;
}

function gotoURL(url) {
	mainWindow.content.wrappedJSObject.location=url;
	//alert(url);
}

function updateSuggestList(urlArray,listName) {
	//clear list
	var ct=0;
	var list = document.getElementById(listName);
    while(list.getRowCount() != 0) {
		list.removeItemAt(0);
    }
	//To populate suggested navigation links sites listbox
	var urlNodes = getNodesFromXPath(urlArray);
	var seenUrl = [];
	for(var i in urlNodes)
	{
		seenUrl[urlNodes[i].href]=1;
	}
	
	LOG('seenurl size is ' + seenUrl.length);
	
	for(var i in seenUrl)
	{
		//LOG('i is ' + i);

		list.appendItem(i);	
		ct++;
		if(ct>12) break;
			
		//debugging
		//list.removeItemAt(0);
		//var val = list.insertItemAt(0,urlNodes[i].href);
		//LOG('inserting ' + urlNodes[i].href);
			
		
	}
	LOG('listname is ' + listName + ' list row count is ' + list.getRowCount());
	LOG('ct is ' + ct);	

}
